<template>
  <div>
    <div>
      <el-form :inline="true" style="margin: 20px">
        <el-form-item label="登录名称:">
          <el-input v-model="queryParam.loginName" placeholder="输入登录名称模糊查询" clearable />
        </el-form-item>
        <el-button type="primary" @click="searchQuery">查询</el-button>
        <el-button @click="searchReset">重置</el-button>
      </el-form>
    </div>
    <div>
      <el-button
        type="primary"
        icon="el-icon-plus"
        style="position:relative;left:20px;top:-25px"
        @click="handleAdd"
      >新增</el-button>

      <el-dropdown style="position:relative;left:30px;top:-25px">
        <el-button>
          批量操作<i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item icon="el-icon-success" @click.native="batchSetStatus('true')">启用</el-dropdown-item>
          <el-dropdown-item icon="el-icon-error" @click.native="batchSetStatus('false')">禁用</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div class="table">
      <!-- table区域 -->
      <el-table
        ref="multipleTable"
        :data="roleList"
        tooltip-effect="dark"
        border
        style="width: 1250px;"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" width="120px" />
        <el-table-column prop="id" label="#" align="center" />
        <el-table-column prop="loginName" label="登录名称" />
        <el-table-column prop="userNumLimit" label="用户数量限制" />
        <el-table-column prop="billsNumLimit" label="单据数据限制" />
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">启动</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" :formatter="carTimeFilter" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="float: right"
        :current-page="current"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        class="page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <!-- Form -->
      <el-dialog :title.sync="dialogTitle" :visible.sync="dialogFormVisible">
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item label="登录名称:" :required="true" prop="loginName">
            <el-input v-model="form.loginName" style="width:600px" placeholder="请输入登录名称" />
          </el-form-item>
          <el-form-item label="用户数量限制:">
            <el-input v-model="form.userNumLimit" style="width:600px" placeholder="请输入用户数量限制" />
          </el-form-item>
          <el-form-item label="单据数量限制:">
            <el-input v-model="form.billsNumLimit" style="width:600px" placeholder="请输入单据数量限制" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false; form = {}">取 消</el-button>
          <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import tenantApi from '@/api/tenant'
import moment from 'moment'

export default {
  data() {
    return {
      // 查询条件
      queryParam: {
        loginName: undefined
      },
      form: {
        id: undefined,
        loginName: undefined,
        userNumLimit: undefined,
        billsNumLimit: undefined
      },
      checkParam: {
        id: '',
        loginName: undefined
      },
      rules: {
        loginName: [{ required: true, message: '请输入登录名称', trigger: 'blur' },
          { validator: this.checkIsNameExist, trigger: 'blur' }]
      },
      roleList: [],
      multipleSelection: [],
      current: 1,
      pageSize: 5,
      total: 0,
      dialogFormVisible: false,
      dialogTitle: ''
    }
  },
  created() {
    this.list()
  },
  methods: {
    batchSetStatus(status) {
      if (this.multipleSelection === '') {
        this.$message.warning('请至少选择一条数据进行修改！')
      } else {
        this.$confirm('是否修改客户状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const arrray = []
          // 遍历勾选数据取自己需要的id等这里是和后端需要的参数对应
          for (let i = 0; i < this.multipleSelection.length; i++) {
            arrray.push(this.multipleSelection[i].id)// 把取出的对象push到数组里
          }
          tenantApi.batchUpdate(status, arrray)
            .then(res => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.list()
            })
        })
      }
    },
    carTimeFilter(row, column, cellValue, index) {
      const daterc = row[column.property]
      if (daterc != null) {
        return moment(daterc).format('YYYY-MM-DD')
      }
    },
    list() {
      tenantApi.list(this.current, this.pageSize, this.queryParam).then((res) => {
        this.roleList = res.data.records
        this.pageSize = res.data.size
        this.current = res.data.current
        this.total = res.data.total
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.dialogTitle = '新增'
      this.form.id = undefined
      this.form = {}
      // 清除自动触发的表单验证
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.list()
    },
    handleCurrentChange(val) {
      this.current = val
      this.list()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    searchQuery() {
      this.list()
    },
    searchReset() {
      this.queryParam = {
        loginName: ''
      }
    },
    checkIsNameExist(rule, value, callback) {
      this.checkParam.id = this.form.id
      this.checkParam.loginName = this.form.loginName
      tenantApi.checkIsNameExist(this.checkParam)
        .then(res => {
          if (res.status) {
            callback(new Error('名称已存在'))
          } else {
            callback()
          }
        })
    },
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          if (this.form.id === undefined) {
            tenantApi.addTenant(this.form)
              .then(res => {
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
                this.form = {}
                this.dialogFormVisible = false
                this.list()
              })
          } else {
            tenantApi.updateTenant(this.form)
              .then(res => {
                this.$message({
                  type: 'success',
                  message: '修改成功'
                })
              })
          }
          this.form = {}
          this.dialogFormVisible = false
          this.list()
        }
      })
    },
    // 查看详细
    edit(row) {
      this.dialogTitle = '修改'
      this.dialogFormVisible = true
      this.form = JSON.parse(JSON.stringify(row))
      // 清除自动触发的表单验证
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    }
  }
}
</script>

<style>
.table {
  position: relative;
  left: 40px;
}

.page {
  position: relative;
  left: -100px;
  top: 10px;
}
</style>
